<?php


namespace app\controller;

use app\BaseController;
use app\model\BlogModel;
use think\Facade\Db;

class Blog extends BaseController
{
    public function index()
    {
        return view();
    }

    public function add()
    {
        return view("add");
    }

    public function addBlog()
    {
        $title = $_GET['title'] ?? "";
        $link = $_GET['link'] ?? "";
        if ($title == "" || $link == "") {
            return json(['code' => 0, 'msg' => '信息不完整']);
        }
        $data = ['title' => $title, 'link' => $link];
//        $result = Db::name('m_blog')->save($data);
        $blog = new BlogModel();
        $result = $blog->save($data);
        if ($result) {
            return json(['code' => 1, 'msg' => 'success', 'data' => $result]);
        } else {
            return json(['code' => 0, 'msg' => '添加失败']);
        }

    }

    public function getBlog()
    {
        $list = BlogModel::order('id', "desc")->select();
        $data = ['code' => 0, 'data' => $list];

        return $data;
    }
}